home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Tools / pynche / PyncheWidget.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  8.3 KB  |  243 lines

  1. """Main Pynche (Pythonically Natural Color and Hue Editor) widget.
  2.  
  3. This window provides the basic decorations, primarily including the menubar.
  4. It is used to bring up other windows.
  5. """
  6.  
  7. import sys
  8. import os
  9. import string
  10. from Tkinter import *
  11. import tkMessageBox
  12.  
  13. # Milliseconds between interrupt checks
  14. KEEPALIVE_TIMER = 500
  15.  
  16.  
  17.  
  18. class PyncheWidget:
  19.     def __init__(self, version, switchboard, master=None):
  20.         self.__sb = switchboard
  21.         self.__version = version
  22.         self.__textwin = None
  23.         self.__listwin = None
  24.         self.__detailswin = None
  25.         self.__helpwin = None
  26.         modal = self.__modal = not not master
  27.         # If a master was given, we are running as a modal dialog servant to
  28.         # some other application.  We rearrange our UI in this case (there's
  29.         # no File menu and we get `Okay' and `Cancel' buttons), and we do a
  30.         # grab_set() to make ourselves modal
  31.         if modal:
  32.             self.__tkroot = tkroot = Toplevel(master, class_='Pynche')
  33.             tkroot.grab_set()
  34.             tkroot.withdraw()
  35.         else:
  36.             # Is there already a default root for Tk, say because we're
  37.             # running under Guido's IDE? :-) Two conditions say no, either the
  38.             # import fails or _default_root is None.
  39.             tkroot = None
  40.             try:
  41.                 from Tkinter import _default_root
  42.                 tkroot = self.__tkroot = _default_root
  43.             except ImportError:
  44.                 pass
  45.             if not tkroot:
  46.                 tkroot = self.__tkroot = Tk(className='Pynche')
  47.             # but this isn't our top level widget, so make it invisible
  48.             tkroot.withdraw()
  49.         # create the menubar
  50.         menubar = self.__menubar = Menu(tkroot)
  51.         #
  52.         # File menu
  53.         #
  54.         if not modal:
  55.             filemenu = self.__filemenu = Menu(menubar, tearoff=0)
  56.             filemenu.add_command(label='Quit',
  57.                                  command=self.__quit,
  58.                                  accelerator='Alt-Q',
  59.                                  underline=0)
  60.         #
  61.         # View menu
  62.         #
  63.         viewmenu = Menu(menubar, tearoff=0)
  64.         viewmenu.add_command(label='Text Window...',
  65.                              command=self.__popup_text,
  66.                              underline=0)
  67.         viewmenu.add_command(label='Color List Window...',
  68.                              command=self.__popup_listwin,
  69.                              underline=0)
  70.         viewmenu.add_command(label='Details Window...',
  71.                              command=self.__popup_details,
  72.                              underline=0)
  73.         #
  74.         # Help menu
  75.         #
  76.         helpmenu = Menu(menubar, name='help', tearoff=0)
  77.     helpmenu.add_command(label='About Pynche...',
  78.                              command=self.__popup_about,
  79.                              underline=0)
  80.         helpmenu.add_command(label='Help...',
  81.                              command=self.__popup_usage,
  82.                              underline=0)
  83.         #
  84.         # Tie them all together
  85.         #
  86.         if not modal:
  87.             menubar.add_cascade(label='File',
  88.                                 menu=filemenu,
  89.                                 underline=0)
  90.         menubar.add_cascade(label='View',
  91.                             menu=viewmenu,
  92.                             underline=0)
  93.         menubar.add_cascade(label='Help',
  94.                             menu=helpmenu,
  95.                             underline=0)
  96.  
  97.         # now create the top level window
  98.         root = self.__root = Toplevel(tkroot, class_='Pynche', menu=menubar)
  99.         root.protocol('WM_DELETE_WINDOW',
  100.                       modal and self.__bell or self.__quit)
  101.         root.title('Pynche %s' % version)
  102.         root.iconname('Pynche')
  103.         # Only bind accelerators for the File->Quit menu item if running as a
  104.         # standalone app
  105.         if not modal:
  106.             root.bind('<Alt-q>', self.__quit)
  107.             root.bind('<Alt-Q>', self.__quit)
  108.         else:
  109.             # We're a modal dialog so we have a new row of buttons
  110.             bframe = Frame(root, borderwidth=1, relief=RAISED)
  111.             bframe.grid(row=4, column=0, columnspan=2,
  112.                         sticky='EW',
  113.                         ipady=5)
  114.             okay = Button(bframe,
  115.                           text='Okay',
  116.                           command=self.__okay)
  117.             okay.pack(side=LEFT, expand=1)
  118.             cancel = Button(bframe,
  119.                             text='Cancel',
  120.                             command=self.__cancel)
  121.             cancel.pack(side=LEFT, expand=1)
  122.  
  123.     def __quit(self, event=None):
  124.         self.__tkroot.quit()
  125.  
  126.     def __bell(self, event=None):
  127.         self.__tkroot.bell()
  128.  
  129.     def __okay(self, event=None):
  130.         self.__sb.withdraw_views()
  131.         self.__tkroot.grab_release()
  132.         self.__quit()
  133.  
  134.     def __cancel(self, event=None):
  135.         self.__sb.canceled()
  136.         self.__okay()
  137.  
  138.     def __keepalive(self):
  139.         # Exercise the Python interpreter regularly so keyboard interrupts get
  140.         # through.
  141.         self.__tkroot.tk.createtimerhandler(KEEPALIVE_TIMER, self.__keepalive)
  142.  
  143.     def start(self):
  144.         if not self.__modal:
  145.             self.__keepalive()
  146.         self.__tkroot.mainloop()
  147.  
  148.     def window(self):
  149.         return self.__root
  150.  
  151.     def __popup_about(self, event=None):
  152.         from Main import __version__
  153.         tkMessageBox.showinfo('About Pynche ' + __version__,
  154.                               '''\
  155. Pynche %s
  156. The PYthonically Natural
  157. Color and Hue Editor
  158.  
  159. For information
  160. contact: Barry A. Warsaw
  161. email:   bwarsaw@python.org''' % __version__)
  162.  
  163.     def __popup_usage(self, event=None):
  164.         if not self.__helpwin:
  165.             self.__helpwin = Helpwin(self.__root, self.__quit)
  166.         self.__helpwin.deiconify()
  167.  
  168.     def __popup_text(self, event=None):
  169.         if not self.__textwin:
  170.             from TextViewer import TextViewer
  171.             self.__textwin = TextViewer(self.__sb, self.__root)
  172.             self.__sb.add_view(self.__textwin)
  173.         self.__textwin.deiconify()
  174.  
  175.     def __popup_listwin(self, event=None):
  176.         if not self.__listwin:
  177.             from ListViewer import ListViewer
  178.             self.__listwin = ListViewer(self.__sb, self.__root)
  179.             self.__sb.add_view(self.__listwin)
  180.         self.__listwin.deiconify()
  181.  
  182.     def __popup_details(self, event=None):
  183.         if not self.__detailswin:
  184.             from DetailsViewer import DetailsViewer
  185.             self.__detailswin = DetailsViewer(self.__sb, self.__root)
  186.             self.__sb.add_view(self.__detailswin)
  187.         self.__detailswin.deiconify()
  188.  
  189.     def withdraw(self):
  190.         self.__root.withdraw()
  191.  
  192.     def deiconify(self):
  193.         self.__root.deiconify()
  194.  
  195.  
  196.  
  197. class Helpwin:
  198.     def __init__(self, master, quitfunc):
  199.         from Main import __version__, docstring
  200.         self.__root = root = Toplevel(master, class_='Pynche')
  201.         root.protocol('WM_DELETE_WINDOW', self.__withdraw)
  202.         root.title('Pynche Help Window')
  203.         root.iconname('Pynche Help Window')
  204.         root.bind('<Alt-q>', quitfunc)
  205.         root.bind('<Alt-Q>', quitfunc)
  206.         root.bind('<Alt-w>', self.__withdraw)
  207.         root.bind('<Alt-W>', self.__withdraw)
  208.  
  209.         # more elaborate help is available in the README file
  210.         readmefile = os.path.join(sys.path[0], 'README')
  211.         try:
  212.             fp = None
  213.             try:
  214.                 fp = open(readmefile)
  215.                 contents = fp.read()
  216.                 # wax the last page, it contains Emacs cruft
  217.                 i = string.rfind(contents, '\f')
  218.                 if i > 0:
  219.                     contents = string.rstrip(contents[:i])
  220.             finally:
  221.                 if fp:
  222.                     fp.close()
  223.         except IOError:
  224.             sys.stderr.write("Couldn't open Pynche's README, "
  225.                              'using docstring instead.\n')
  226.             contents = docstring()
  227.  
  228.         self.__text = text = Text(root, relief=SUNKEN,
  229.                                   width=80, height=24)
  230.         self.__text.focus_set()
  231.         text.insert(0.0, contents)
  232.         scrollbar = Scrollbar(root)
  233.         scrollbar.pack(fill=Y, side=RIGHT)
  234.         text.pack(fill=BOTH, expand=YES)
  235.         text.configure(yscrollcommand=(scrollbar, 'set'))
  236.         scrollbar.configure(command=(text, 'yview'))
  237.  
  238.     def __withdraw(self, event=None):
  239.         self.__root.withdraw()
  240.  
  241.     def deiconify(self):
  242.         self.__root.deiconify()
  243.